package com.weetop.akly.activity.login;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.weetop.akly.R;
import com.weetop.akly.activity.MainActivity;
import com.weetop.akly.base.BaseActivity;
import com.weetop.akly.base.mvp.BasePresenter;
import com.weetop.akly.utils.BaseUtils;
import com.weetop.akly.utils.MMKVUtils;
import com.weetop.akly.utils.StarView.StringUtils;

import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

public class FlashActivity extends BaseActivity {

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flash);
        checkPermission();
    }

    private void checkPermission() {
        PermissionUtils.permission(PermissionConstants.STORAGE, PermissionConstants.LOCATION, PermissionConstants.PHONE)
                .callback(new PermissionUtils.FullCallback() {
                    @Override
                    public void onGranted(List<String> permissionsGranted) {
                        toLogin();
                    }

                    @Override
                    public void onDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied) {
                        ToastUtils.showShort("部分权限被禁用，相关功能无法使用");
                        toLogin();
                    }
                }).request();
    }

    /**
     * 跳转登录/自动登录
     */
    private void toLogin() {
        if (!TextUtils.isEmpty(MMKVUtils.getUserToken()) && !TextUtils.isEmpty(MMKVUtils.getRyToken())) {
            //融云   后期接入
            RongIM.connect(MMKVUtils.getRyToken(), new RongIMClient.ConnectCallback() {
                @Override
                public void onTokenIncorrect() {
                    Log.e(TAG, "onTokenIncorrect: 融云单聊连接token错误----Token：" + MMKVUtils.getRyToken());
                }

                @Override
                public void onSuccess(String userid) {
                    Log.e(TAG, "onSuccess: 融云单聊连接成功----userId：" + userid);
                }

                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {
                    Log.e(TAG, "onError: 融云单聊连接失败----errorCode：" + errorCode.getValue() + ",message：" + errorCode.getMessage());
                }
            });
        }
        new CountDownTimer(2000, 1000) {
            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                if (StringUtils.isNotEmpty(MMKVUtils.getUserToken())) {
                    BaseUtils.toActivity(mActivity, MainActivity.class);
                    finish();
                } else {
                    BaseUtils.toActivity(mActivity, LoginActivity.class);
                    finish();
                }
            }
        }.start();
    }
}
